using System.IO;
using System.IO.Compression;
using System.Text;

namespace Noria.Web.Utility
{
    internal class GZipHelper
    {
        public static byte[] Compress(string raw)
        {
            return Compress(Encoding.UTF8.GetBytes(raw));
        }

        public static byte[] Compress(byte[] raw)
        {
            using (var mos = new MemoryStream())
            {
                using (var mis = new MemoryStream(raw))
                {
                    using (var gs = new GZipStream(mos, CompressionMode.Compress))
                    {
                        mis.CopyTo(gs);
                    }
                }
                return mos.ToArray();
            }
        }


        public static byte[] Decompress(byte[] raw)
        {
            using (var mos = new MemoryStream())
            {
                using (var mis = new MemoryStream(raw))
                {
                    using (var gs = new GZipStream(mis, CompressionMode.Decompress))
                    {
                        gs.CopyTo(mos);
                    }
                }
                return mos.ToArray();
            }
        }
    }
}